<template>
  <div>
    <van-nav-bar
        :title=page_title
        placeholder
        fixed
        left-arrow
        @click-left="goBack"
    />
    <van-address-edit
        :area-list="areaList"
        show-set-default
        show-search-result
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        @change-default="onChangeDefault"
        :address-info="{
        name:item.name,
        tel:item.tel,
        areaCode: areaCode,
        addressDetail: item.addressDetail,
        isDefault:item.isDefault
      }"
    />
  </div>
</template>

<script>
import { areaList } from "@vant/area-data";
import _ from 'lodash';

export default {
  data() {
    return {
      areaCode:'',
      id:'',
      user_id:'',
      page_title: '你好',
      areaList,
      T_default:'0',
      searchResult: [],
      item:{
        name:'',
        tel:'',
        province:'',
        city:'',
        county:'',
        addressDetail:'',
        isDefault:'',
        type:'3',
        areaCode:'',
      },
      // username:sessionStorage.getItem("username")
    };
  },
  methods: {
    goBack() {
      this.$router.push("my-return-address?user_id="+this.user_id);
    },
    onSave(content) {
      var _this = this
      content.user_id = _this.item.user_id
      content.type = 3
      content.isDefault = _this.T_default
      if(!_this.id){//添加操作
        this.axios({
          method:'post',
          url:'/address_edit/addressAdd',
          data:content
        })
        sessionStorage.setItem('reload',"1")
      }
      else if(_this.id){//修改操作
        content.user_id = _this.user_id
        content.type = _this.item.type
        content.id = _this.id
        this.axios({
          method:'post',
          url:'/address_edit/addressEdit/',
          data:content
        })
        sessionStorage.setItem('reload',"2")
      }
      this.$router.push("my-return-address?user_id="+_this.user_id);
    },
    onChangeDefault(val){
      if(val==true){
        this.T_default=1
      }
      else if(val==false){
        this.T_default=0
      }
    }
  },
  mounted() {
    var _this = this
    _this.id = _this.$route.query.id
    _this.user_id = _this.$route.query.user_id
    if(_this.id){
      _this.page_title='编辑地址'
      this.axios({
        method:'get',
        url:'/address_edit/addressById/'+_this.id,
      }).then(function (rep){
        _this.item = rep.data
        _this.T_default = _this.item.isDefault

        //省市区回显
        var city = _this.item.city; //城市
        var district = _this.item.county; //区/县
        _.forEach(_this.areaList.city_list, (o, c) => {
          if (o == city) {
            let cityId = String(_.take(c, 2));
            _.forEach(_this.areaList.county_list, (i, a) => {
              if (i == district) {
                let districtId = String(_.take(a, 2));
                if (cityId == districtId) {
                  _this.areaCode = a;
                  // alert(_this.areaCode)
                }
              }
            });
          }
        });


        if(_this.item.isDefault==1){
          _this.item.isDefault=true
        }
        else if(_this.item.isDefault==0){
          _this.item.isDefault=false
        }
      })
    }
    else {
      _this.page_title='添加地址'
    }
    _this.item.user_id = _this.user_id
  },

};
</script>

<style scoped>

</style>